//
//  RSWDataOperation+Workflow.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 2017/8/25.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "RSWDataOperation+Workflow.h"
#import "RSWRESTService+Workflow.h"

@implementation RSWDataOperation (Workflow)

-(void)getAllWorkflowDate:(NSString*)date
                pageIndex:(NSInteger)pageIndex
                 pageSize:(NSInteger)pageSize
                     code:(NSString*)code
               completion:(void(^)(NSInteger nResult,XMSQueueDictionary *dic,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
    {
        [self pullRefreshViewBegin];
        
        XLGResult *r = nil;
        RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
        if(ser != nil)
        {
            NSString *token = [XLGContext context].m_token;
            
            r = [ser getALlWorkflowDate:date token:token pageIndex:pageIndex pageSize:pageSize code:code];
            
            [self detachDataOperationManager:@[ser]];
        }
        
        [self requestResult:r.outResult output:r.outObj error:r.outError completion:completion];
        
        [self pullRefreshViewEnd];
    }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,nil,nil);
}

-(void)getToDoWorkflowDate:(NSString*)date
                pageIndex:(NSInteger)pageIndex
                 pageSize:(NSInteger)pageSize
                     code:(NSString*)code
               completion:(void(^)(NSInteger nResult,XMSQueueDictionary *dic,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
    {
        [self pullRefreshViewBegin];
        
        XLGResult *r = nil;
        RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
        if(ser != nil)
        {
            NSString *token = [XLGContext context].m_token;
            
            r = [ser getToDoWorkflowDate:date token:token pageIndex:pageIndex pageSize:pageSize code:code];
            
            [self detachDataOperationManager:@[ser]];
        }
        
        [self requestResult:r.outResult output:r.outObj error:r.outError completion:completion];
        
        [self pullRefreshViewEnd];
    }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,nil,nil);
}

-(void)getWorkflowApply:(NSString*)processcode
             customform:(NSString*)customform
                version:(NSString*)version
             completion:(void(^)(NSInteger nResult,id output,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
    {
        [self pullRefreshViewBegin];
        
        XLGResult *r = nil;
        WorkflowApplyInfo *applyInfo = [WorkflowApplyInfo new];
        RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
        if(ser != nil)
        {
            NSString *token = [XLGContext context].m_token;
            
            r = [ser getWorkflowApply:processcode token:token customform:customform version:version];
            
            if(r.outResult == YES && r.outObj != nil)
                applyInfo = [self processData:r];
            
            [self detachDataOperationManager:@[ser]];
        }
        
        [self requestResult:r.outResult output:applyInfo  error:r.outError completion:completion];
        
        [self pullRefreshViewEnd];
    }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,nil,nil);
}

-(void)getWorkflowApprove:(NSString*)processcode
               customform:(NSString*)customform
                  version:(NSString*)version
               instanceid:(NSString*)instanceid
               completion:(void(^)(NSInteger nResult,id output,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
    {
        [self pullRefreshViewBegin];
        
        XLGResult *r = nil;
        WorkflowApplyInfo *applyInfo = [WorkflowApplyInfo new];
        RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
        if(ser != nil)
        {
            NSString *token = [XLGContext context].m_token;
            
            r = [ser getWorkflowApprove:processcode token:token customform:customform version:version instanceid:instanceid];
            
            if(r.outResult == YES && r.outObj != nil)
                applyInfo = [self processData:r];
            
            [self detachDataOperationManager:@[ser]];
        }
        
        [self requestResult:r.outResult output:applyInfo  error:r.outError completion:completion];
        
        [self pullRefreshViewEnd];
    }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,nil,nil);
}

#pragma --- mark
#pragma --- mark private method

-(NSArray*)uploadFile:(NSArray*)attachment modelId:(NSString*)modelId
{
    NSMutableArray *ar = [NSMutableArray new];
    NSArray *paras = [NSArray arrayWithObject:[NSString stringWithFormat:@"&modelid=%@",modelId]];
    NSArray *fileIdArray = [self uploadImage:attachment paras:paras];
    
    if(fileIdArray.count > 0 && (fileIdArray.count == attachment.count))
    {
        for (NSInteger i = 0; i < attachment.count; i++)
        {
            NSString *fileId = [[attachment[i] lastPathComponent] stringByDeletingPathExtension];
            NSMutableDictionary *dic = [NSMutableDictionary new];
            [dic setValue:modelId forKey:@"ModelID"];
            [dic setValue:fileId forKey:@"ID"];
            [dic setValue:[attachment[i] lastPathComponent] forKey:@"FielFullName"];
            [dic setValue:[attachment[i] pathExtension] forKey:@"FileExtension"];
            [dic setValue:@"" forKey:@"FileFolder"];
            [dic setValue:@"" forKey:@"FilePreName"];
            [dic setValue:@(0) forKey:@"FileSize"];
            [dic setValue:@(0) forKey:@"Status"];
            [dic setValue:[[NSDate date] toStringDate:yMdHms] forKey:@"CreateDate"];
            
            [ar addObject:dic];
        }
    }
    
    return ar;
}

-(NSArray*)orgAttachment:(NSArray*)attachment modelId:(NSString*)modelId
{
    NSMutableArray *ar = [NSMutableArray new];
    for (NSInteger i = 0; i < attachment.count; i++)
    {
        WorkflowAttachmentInfo *att = attachment[i];
        NSMutableDictionary *dic = [NSMutableDictionary new];
        [dic setValue:att.modelID forKey:@"ModelID"];
        [dic setValue:att.attachmentId forKey:@"ID"];
        [dic setValue:att.fielFullName forKey:@"FielFullName"];
        [dic setValue:att.fileExtension forKey:@"FileExtension"];
        [dic setValue:att.fileFolder forKey:@"FileFolder"];
        [dic setValue:att.filePreName forKey:@"FilePreName"];
        [dic setValue:@([att.fileSize intValue]) forKey:@"FileSize"];
        [dic setValue:@([att.status intValue]) forKey:@"Status"];
        [dic setValue:[att.createDate toStringDate:yMdHms] forKey:@"CreateDate"];
        
        [ar addObject:dic];
    }
    
    return ar;
}

-(id)processData:(XLGResult *)r
{
    WorkflowApplyInfo *applyInfo = [WorkflowApplyInfo new];
    
    id customForm = r.outObj[@"CustomForm"];
    
    XMSQueueDictionary *qdSet = [CustomerFormInfo JSONObjectProperty:nil];
    if(customForm != nil && [customForm isKindOfClass:[NSNull class]] == NO)
    {
        NSArray *ar =[CTransformHTTPRESTObj JSONObjs:@[customForm] transformToProtocolObj:[CustomerFormInfo class] accordingToColPropertyPair:qdSet];
        if(ar.count > 0)
        {
            applyInfo.customForm = [ar mutableCopy][0];
        }
    }
    
    id fields = r.outObj[@"Fields"];
    qdSet = [CustomerFormFieldInfo JSONObjectProperty:nil];
    if(fields != nil && [fields isKindOfClass:[NSArray class]] == YES)
    {
        NSArray *ar =[CTransformHTTPRESTObj JSONObjs:fields transformToProtocolObj:[CustomerFormFieldInfo class] accordingToColPropertyPair:qdSet];
        if(ar.count > 0)
        {
            applyInfo.fields = [ar mutableCopy];
        }
    }
    
    id activityFields = r.outObj[@"ActivityFields"];
    qdSet = [ActivityFieldInfo JSONObjectProperty:nil];
    if(activityFields != nil && [activityFields isKindOfClass:[NSArray class]] == YES)
    {
        NSArray *ar =[CTransformHTTPRESTObj JSONObjs:activityFields transformToProtocolObj:[ActivityFieldInfo class] accordingToColPropertyPair:qdSet];
        if(ar.count > 0)
        {
            applyInfo.activityFields = [ar mutableCopy];
        }
    }
    
    id fieldConfigItems = r.outObj[@"FieldConfigItems"];
    qdSet = [FieldConfigItemInfo JSONObjectProperty:nil];
    if(fieldConfigItems != nil && [fieldConfigItems isKindOfClass:[NSArray class]] == YES)
    {
        NSArray *ar =[CTransformHTTPRESTObj JSONObjs:fieldConfigItems transformToProtocolObj:[FieldConfigItemInfo class] accordingToColPropertyPair:qdSet];
        if(ar.count > 0)
        {
            applyInfo.fieldConfigItems = [ar mutableCopy];
        }
    }
    
    id permission = r.outObj[@"Permission"];
    qdSet = [WorkflowPermissionInfo JSONObjectProperty:nil];
    if(permission != nil && [permission isKindOfClass:[NSNull class]] == NO)
    {
        NSArray *ar =[CTransformHTTPRESTObj JSONObjs:@[permission] transformToProtocolObj:[WorkflowPermissionInfo class] accordingToColPropertyPair:qdSet];
        if(ar.count > 0)
        {
            applyInfo.permission = [ar mutableCopy][0];
            applyInfo.permission.nextActionNames = permission[@"NextActionNames"];
        }
    }
    
    id attachment = r.outObj[@"Attachment"];
    qdSet = [WorkflowAttachmentInfo JSONObjectProperty:nil];
    if(attachment != nil && [attachment isKindOfClass:[NSArray class]] == YES)
    {
        NSArray *ar =[CTransformHTTPRESTObj JSONObjs:attachment transformToProtocolObj:[WorkflowAttachmentInfo class] accordingToColPropertyPair:qdSet];
        if(ar.count > 0)
        {
            applyInfo.attachments = [ar mutableCopy];
        }
    }
    
    applyInfo.processGUID = r.outObj[@"ProcessGUID"];
    applyInfo.processName = r.outObj[@"ProcessName"];
    applyInfo.version = r.outObj[@"Version"];
    applyInfo.nextActionNames = r.outObj[@"NextActionNames"];
    applyInfo.modelValue = r.outObj[@"ModelValue"];
    applyInfo.isMyPendingTask = [r.outObj[@"IsMyPendingTask"] boolValue];
    
    return applyInfo;
}

-(void)getStepRoleUserTree:(NSString*)processcode
                     version:(NSString*)version
                         dic:(NSDictionary*)dic
             workflowOptType:(NSInteger)workflowOptType
                  completion:(void(^)(NSInteger nResult,id output,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
    {
        [self pullRefreshViewBegin];
        
        XLGResult *r = nil;
        WorkflowSelectUserInfo *selectUserInfo = [WorkflowSelectUserInfo new];
        RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
        if(ser != nil)
        {
            NSString *token = [XLGContext context].m_token;
            NSString *url = @"";
            if(workflowOptType == WorkflowOptTypeApply)
                url = @"FirstStepRoleUserTree";
            else if(workflowOptType == WorkflowOptTypeAudit)
                url = @"NextStepRoleUserTree";
            
            r = [ser getStepRoleUserTree:processcode token:token version:version dic:dic url:url];
            
            id selectUser = r.outObj;
            
            XMSQueueDictionary *qdSet = [WorkflowSelectUserInfo JSONObjectProperty:nil];
            if([selectUser isKindOfClass:[NSNull class]] == NO)
            {
                NSArray *ar =[CTransformHTTPRESTObj JSONObjs:@[selectUser] transformToProtocolObj:[WorkflowSelectUserInfo class] accordingToColPropertyPair:qdSet];
                if(ar.count > 0)
                {
                    selectUserInfo = [ar mutableCopy][0];
                }
            }
            
            id entity = r.outObj[@"Entity"];
            qdSet = [WorkflowUserEntityInfo JSONObjectProperty:nil];
            if([entity isKindOfClass:[NSArray class]] == YES)
            {
                NSArray *ar =[CTransformHTTPRESTObj JSONObjs:entity transformToProtocolObj:[WorkflowUserEntityInfo class] accordingToColPropertyPair:qdSet];
                if(ar.count > 0)
                {
                    selectUserInfo.entity = [ar mutableCopy];
                }
                
                if(selectUserInfo.entity.count > 0)
                {
                    for (NSInteger i = 0; i < selectUserInfo.entity.count; i ++)
                    {
                        id participants = entity[i][@"Participants"];
                        qdSet = [WorkflowParticipantInfo JSONObjectProperty:nil];
                        if([participants isKindOfClass:[NSArray class]] == YES)
                        {
                            ar =[CTransformHTTPRESTObj JSONObjs:participants transformToProtocolObj:[WorkflowParticipantInfo class] accordingToColPropertyPair:qdSet];
                            if(ar.count > 0)
                            {
                                ((WorkflowUserEntityInfo*)selectUserInfo.entity[i]).participants = [ar mutableCopy];
                            }
                            
                            if(((WorkflowUserEntityInfo*)selectUserInfo.entity[i]).participants.count > 0)
                            {
                                for (NSInteger j = 0; j < ((WorkflowUserEntityInfo*)selectUserInfo.entity[i]).participants.count; j ++)
                                {
                                    id users = participants[j][@"Users"];
                                    qdSet = [WorkflowUserInfo JSONObjectProperty:nil];
                                    if([users isKindOfClass:[NSArray class]] == YES)
                                    {
                                        ar =[CTransformHTTPRESTObj JSONObjs:users transformToProtocolObj:[WorkflowUserInfo class] accordingToColPropertyPair:qdSet];
                                        if(ar.count > 0)
                                        {
                                            ((WorkflowParticipantInfo*)(((WorkflowUserEntityInfo*)selectUserInfo.entity[i]).participants)[j]).users = [ar mutableCopy];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
            [self detachDataOperationManager:@[ser]];
        }
        
        [self requestResult:r.outResult output:selectUserInfo  error:r.outError completion:completion];
        
        [self pullRefreshViewEnd];
    }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,nil,nil);
}

-(void)startWorkflowAppInstance:(NSString*)processcode
                        version:(NSString*)version
                            dic:(NSMutableDictionary*)dic
                     completion:(void(^)(NSInteger nResult,BOOL result,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
    {
        [self progressViewBeginWithTip:@"提交中，请稍候"];
        
        XLGResult *r = nil;
        NSString *token = [XLGContext context].m_token;
        RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
        if(ser != nil)
        {
            NSMutableArray *attach = dic[@"Model"][@"attachment"];
            NSString *modelId = dic[@"Model"][@"ID"];
            
            //附件上传
            NSArray *ar = [self uploadFile:attach modelId:modelId];
            if(ar.count <= 0 && attach.count > 0)
            {
                NSError *err = [NSError errorWithDomain:@"上传文件失败" code:0 userInfo:nil]; 
                r = [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:err];
            }
            else
            {
                dic[@"Model"][@"attachment"] = ar;
                r = [ser startWorkflowAppInstance:processcode token:token version:version dic:dic];
            }
            [self detachDataOperationManager:@[ser]];
        }
        
        [self requestResult:r.outResult output:r.outObj error:r.outError completion:^(NSInteger nResult, id output, NSError *error)
         {
             completion(nResult,[output boolValue],error);
         }];
        
        [self progressViewEnd];
    }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,NO,nil);
}

NSString *const RSWDataOperationWorkflowProcessNextNotification =
                @"RSWDataOperationWorkflowProcessNextNotification";

-(void)nextWorkflowAppInstance:(NSString*)processcode
                        version:(NSString*)version
                            dic:(NSMutableDictionary*)dic
                    attachment:(NSArray*)attachment
                     completion:(void(^)(NSInteger nResult,BOOL result,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
    {
        [self progressViewBeginWithTip:@"提交中，请稍候"];
        
        XLGResult *r = nil;
        NSString *token = [XLGContext context].m_token;
        RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
        if(ser != nil)
        { 
            NSMutableArray *attach = dic[@"Model"][@"attachment"];
            NSString *modelId = dic[@"Model"][@"ID"];
            
            NSMutableArray *oldAttachment = [attachment mutableCopy];
            NSArray *attachment_copy = [attach mutableCopy];
            [attach removeAllObjects];
            if(oldAttachment.count > 0)
            {
                NSArray *fileIdArray = [oldAttachment fetchArrayObjectPorpertyValueWithPorperty:@"attachmentId"];
                for (NSInteger i = 0; i < attachment_copy.count; i++)
                {
                    NSString *fileId = [[attachment_copy[i] lastPathComponent] stringByDeletingPathExtension];
                    if([fileIdArray containsObject:fileId] == NO)
                    {
                        [attach addObject:attachment_copy[i]];
                    }
                }
            }
            
            NSMutableArray *fileIdArray = [NSMutableArray new];
            for (NSInteger i = 0; i < attachment_copy.count; i++)
            {
                [fileIdArray addObject:[[attachment_copy[i] lastPathComponent] stringByDeletingPathExtension]];
            }
            
            NSMutableArray *existAttachment = [NSMutableArray new];
            for (NSInteger i = 0; i < oldAttachment.count; i++)
            {
                WorkflowAttachmentInfo *att = oldAttachment[i];
                if([fileIdArray containsObject:att.attachmentId])
                {
                    [existAttachment addObject:att];
                }
            }
            
            //附件上传
            NSMutableArray *ar = [[self uploadFile:attach modelId:modelId] mutableCopy];
            if(attach.count >0 && ar.count <= 0)
            {
                NSError *err = [NSError errorWithDomain:@"上传文件失败" code:0 userInfo:nil];
                r = [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:err];
            }
            else
            {
                NSArray *arr = [self orgAttachment:existAttachment modelId:modelId];
                [ar addObjectsFromArray:arr];
                dic[@"Model"][@"attachment"] = ar;
                r = [ser nextWorkflowAppInstance:processcode token:token version:version dic:dic];
            }
            [self detachDataOperationManager:@[ser]];
            
            if(r.outResult == YES && r.outObjBool == YES)
            {
                [NSNotificationCenter postNotification:RSWDataOperationWorkflowProcessNextNotification userInfo:@{}];
            }
        }
        
        [self requestResult:r.outResult output:r.outObj error:r.outError completion:^(NSInteger nResult, id output, NSError *error)
         {
             completion(nResult,[output boolValue],error);
         }];
        
        [self progressViewEnd];
    }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,NO,nil);
}

-(void)withDrawWorkflowAppInstance:(NSString*)processcode
                           version:(NSString*)version
                           comment:(NSString*)comment
                           modelid:(NSString*)modelid
                        completion:(void(^)(NSInteger nResult,BOOL result,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
    {
        [self progressViewBeginWithTip:@"提交中，请稍候"];
        
        XLGResult *r = nil;
        NSString *token = [XLGContext context].m_token;
        RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
        if(ser != nil)
        {
            r = [ser withDrawWorkflowAppInstance:processcode token:token version:version comment:comment modelid:modelid];
            [self detachDataOperationManager:@[ser]];
            
            if(r.outResult == YES && r.outObjBool == YES)
            {
                [NSNotificationCenter postNotification:RSWDataOperationWorkflowProcessNextNotification userInfo:@{}];
            }
        }
        
        [self requestResult:r.outResult output:r.outObj error:r.outError completion:^(NSInteger nResult, id output, NSError *error)
         {
             completion(nResult,[output boolValue],error);
         }];
        
        [self progressViewEnd];
    }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,NO,nil);
}

-(void)sendBackWorkflowAppInstance:(NSString*)processcode
                           version:(NSString*)version
                           comment:(NSString*)comment
                           modelid:(NSString*)modelid
                        completion:(void(^)(NSInteger nResult,BOOL result,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
    {
        [self progressViewBeginWithTip:@"提交中，请稍候"];
        
        XLGResult *r = nil;
        NSString *token = [XLGContext context].m_token;
        RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
        if(ser != nil)
        {
            r = [ser sendBackWorkflowAppInstance:processcode token:token version:version comment:comment modelid:modelid];
            [self detachDataOperationManager:@[ser]];
            
            if(r.outResult == YES && r.outObjBool == YES)
            {
                [NSNotificationCenter postNotification:RSWDataOperationWorkflowProcessNextNotification userInfo:@{}];
            }
        }
        
        [self requestResult:r.outResult output:r.outObj error:r.outError completion:^(NSInteger nResult, id output, NSError *error)
         {
             completion(nResult,[output boolValue],error);
         }];
        
        [self progressViewEnd];
    }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,NO,nil);
}

-(void)discardWorkflowAppInstance:(NSString*)processcode
                          version:(NSString*)version
                          comment:(NSString*)comment
                          modelid:(NSString*)modelid
                       completion:(void(^)(NSInteger nResult,BOOL result,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
    {
        [self progressViewBeginWithTip:@"提交中，请稍候"];
        
        XLGResult *r = nil;
        NSString *token = [XLGContext context].m_token;
        RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
        if(ser != nil)
        {
            r = [ser discardWorkflowAppInstance:processcode token:token version:version comment:comment modelid:modelid];
            [self detachDataOperationManager:@[ser]];
            
            if(r.outResult == YES && r.outObjBool == YES)
            {
                [NSNotificationCenter postNotification:RSWDataOperationWorkflowProcessNextNotification userInfo:@{}];
            }
        }
        
        [self requestResult:r.outResult output:r.outObj error:r.outError completion:^(NSInteger nResult, id output, NSError *error)
         {
             completion(nResult,[output boolValue],error);
         }];
        
        [self progressViewEnd];
    }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,NO,nil);
}

-(void)reverseWorkflowAppInstance:(NSString*)processcode
                          version:(NSString*)version
                          comment:(NSString*)comment
                          modelid:(NSString*)modelid
                       completion:(void(^)(NSInteger nResult,BOOL result,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
    {
        [self progressViewBeginWithTip:@"提交中，请稍候"];
        
        XLGResult *r = nil;
        NSString *token = [XLGContext context].m_token;
        RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
        if(ser != nil)
        {
            r = [ser reverseWorkflowAppInstance:processcode token:token version:version comment:comment modelid:modelid];
            [self detachDataOperationManager:@[ser]];
            
            if(r.outResult == YES && r.outObjBool == YES)
            {
                [NSNotificationCenter postNotification:RSWDataOperationWorkflowProcessNextNotification userInfo:@{}];
            }
        }
        
        [self requestResult:r.outResult output:r.outObj error:r.outError completion:^(NSInteger nResult, id output, NSError *error)
         {
             completion(nResult,[output boolValue],error);
         }];
        
        [self progressViewEnd];
    }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,NO,nil);
}

-(void)getWorkflowHistoryModelId:(NSString*)modelId 
                      completion:(void(^)(NSInteger nResult,NSArray *ar,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
    {
        [self pullRefreshViewBegin];
        
        XLGResult *r = nil;
        RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
        if(ser != nil)
        {
            NSString *token = [XLGContext context].m_token;
            
            r = [ser getWorkflowHistoryModelId:modelId token:token];
            
            [self detachDataOperationManager:@[ser]];
        }
        
        [self requestResult:r.outResult output:r.outObj error:r.outError completion:completion];
        
        [self pullRefreshViewEnd];
    }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,nil,nil);
}
@end
